/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package researchgrants.parts.panelsManager;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JPanel;
import researchgrants.parts.LoggedData.panels.LoggedDataPanels;
import javax.swing.event.ChangeEvent;
import researchgrants.parts.LoggedData.panels.events.ChangeEventHandler;
import javax.swing.event.ChangeListener;
import researchgrants.parts.panels.Panels;
import researchgrants.utils.Constants;

/**
 *
 * @author DOStudent1
 */
public class LoggedDataPanelsManager extends PanelsManager implements ChangeListener{

    boolean isInViewMode=true;
    boolean isNew=false;
    
    @Override
    public void startAsNew(boolean isNew) {
        this.isNew=isNew;
    }

    protected JButton btnSave;

    @Override
    public void display() {
        if (isNew) {
            add(((LoggedDataPanels)panels).getEditComponent());
        } else {
            if (isInViewMode) {
                switchToViewMode();
            } else {
                switchToEditMode();
            }
        }
        setVisible(true);
    }

    
    private void switchToEditMode() {
        isInViewMode=false;
        removeAll();
        JPanel pnlEdit=getEditMode();
        add(pnlEdit);
        updateDisplay();
    }

    private void switchToViewMode() {
        isInViewMode=true;
        removeAll();
        JPanel pnlView=getViewMode();
        add(pnlView);
        updateDisplay();
    }
      
    private JPanel getEditMode() {
        if (panels instanceof ChangeEventHandler) {
            ((ChangeEventHandler)panels).addChangeListener(this);
        }
        
        JPanel editContainer=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
        editContainer.setVisible(true);

        
        JPanel buttonsPanel=new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));

        btnSave=new JButton("Save");
        btnSave.setMargin(Constants.smallButtonMargins);
        btnSave.setFont(Constants.smallButtonFont);
        btnSave.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                saveData();
            }
        });
        buttonsPanel.add(btnSave);

        JButton btnCancel=new JButton("Cancel");
        btnCancel.setMargin(Constants.smallButtonMargins);
        btnCancel.setFont(Constants.smallButtonFont);
        btnCancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                switchToViewMode();
            }
        });
        buttonsPanel.add(btnCancel);
        
        editContainer.add(((LoggedDataPanels)panels).getEditComponent());
        editContainer.add(buttonsPanel);
        
        return(editContainer);
    }

    private JPanel getViewMode() {
        JPanel viewContainer=new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 0));
        viewContainer.setVisible(true);

        viewContainer.add(((LoggedDataPanels)panels).getViewComponent());

        JButton btnEdit=new JButton("Edit");
        btnEdit.setMargin(Constants.smallButtonMargins);
        btnEdit.setFont(Constants.smallButtonFont);
        btnEdit.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                switchToEditMode();
            }
        });
        viewContainer.add(btnEdit);
        add(viewContainer);
        return(viewContainer);
    }

    @Override
    public void saveData() {
        ((LoggedDataPanels)panels).saveData();
        switchToViewMode();
    }
    
    
    @Override
    public void setPanels(Panels panels) {
        if (!(panels instanceof LoggedDataPanels)) {
            throw new UnsupportedOperationException("LoggedDataPanelsManager must be initialized using LoggedDataPanels");
        }
        super.setPanels(panels);
    }

    
    boolean isMandatory=false;
    @Override
    public void setMandatory(boolean isMandatory) {
        this.isMandatory=isMandatory;
    }

    @Override
    public void stateChanged(ChangeEvent e) {
        if (!isInViewMode) {
            if (isMandatory) {
                btnSave.setEnabled(panels.isValid() && (!panels.isEmpty()));
            } else {
                btnSave.setEnabled(panels.isValid());
            }
        }
    }

    
}

